var base = require('/base_info.js');
var utils = require('/utils/index.js');
Page({
  data: {
    activeIndex: 0,
    rise: '',
    duty: '',
    company_address: '',
    phone: '',
    bank: '',
    bankAccount: '',
    roomNumber: '',
    remark: '',
    invoiceInfo: {}
  },
  // 点击导航切换
  navClick (e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.id,
      rise: '',
      duty: '',
      company_address: '',
      phone: '',
      bank: '',
      bankAccount: '',
      roomNumber: '',
      remark: ''
    });
  },
  // 获取发票抬头
  invoiceTitle () {
    let that = this;
    my.chooseInvoiceTitle({
      success: (res) => {
        that.getRiseTitle(res.result.dynamicCode);
      },
      fail: (res) => {
        my.alert({
          content: '获取发票抬头失败'
        });
      },
    });
  },
  getRiseTitle (val) {
    let that = this;
    my.showLoading();
    utils.getData('GetInvoice', {
      dynamic_code: val
    }, (res) => {
      my.hideLoading();
      if (res.data.data.title.title_type == 'PERSONAL') {
        that.setData({
          rise: res.data.data.title.title_name,
          activeIndex: 1
        });
        return false;
      }
      that.setData({
        activeIndex: 0,
        rise: res.data.data.title.title_name ? res.data.data.title.title_name : '',
        duty: res.data.data.title.tax_register_no ? res.data.data.title.tax_register_no : '',
        bank: res.data.data.title.open_bank_name ? res.data.data.title.open_bank_name : '',
        bankAccount: res.data.data.title.open_bank_account ? res.data.data.title.open_bank_account : '',
        company_address: res.data.data.title.user_address ? res.data.data.title.user_address : '',
        phone: res.data.data.title.user_mobile ? res.data.data.title.user_mobile : '',
      })
    }, fail => {
      my.hideLoading();
      my.alert({
        content: '获取发票抬头失败'
      });
    })
  },
  // 房间号监听
  roomNumberInput (e) {
    console.log(e.detail.value);
    this.setData({
      roomNumber: e.detail.value
    });
  },
  // remark监听
  timeInput (e) {
    console.log(e.detail.value);
    this.setData({
      remark: e.detail.value
    }); 
  },
  // 监听抬头改变
  riseInput (e) {
    this.setData({
      rise: e.detail.value
    })
  },
  // 监听税号输入
  dutyInput (e) {
    this.setData({
      duty: e.detail.value
    })
  },
  // 监听地址输入
  addressInput (e) {
    this.setData({
      company_address: e.detail.value
    })
  },
  // 监听手机号码输入
  phoneInput (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 监听开户行输入
  bankInput (e) {
    this.setData({
      bank: e.detail.value
    })
  },
  // 监听银行账户输入
  bankAccountInput (e) {
    this.setData({
      bankAccount: e.detail.value
    });
  },
  // 开发票
  submit () {
    let that = this;
    if (that.data.rise == '') {
      my.alert({
        content: '请填写发票抬头' 
      });
      return false;
    } else if (that.data.duty == '' && that.data.type == 0) {
      my.alert({
        content: '请填写税号' 
      });
      return false;
    } else if(that.data.roomNumber == '') {
      my.alert({
        content: '请输入房间号' 
      });
      return false;
    };
    my.showLoading();
    utils.getData('SingleForwardInvoice', {
      user_id: my.getStorageSync({
        key: 'uid',
      }).data,
      type: that.data.activeIndex * 1 + 1,
      invoice_title: that.data.rise,
      duty_no: that.data.duty,
      company_address: that.data.company_address,
      tel: that.data.phone,
      deposit_bank: that.data.bank, 
      bank_account: that.data.bankAccount,
      room_number: that.data.roomNumber,
      remark: that.data.remark
    }, (res) => {
      my.hideLoading();
      if (res.data.message == 'success') {
        my.setStorageSync({
          key: 'invoice', // 缓存数据的key
          data: '', // 要缓存的数据
        });
        var rise = that.data.rise;
        that.setData({
          rise: '',
          duty: '',
          bank: '',
          bankAccount: '',
          company_address: '',
          phone: '',
          remark: '',
          roomNumber: ''
        });
        my.navigateTo({
          url: './invoce-success?rise=' + rise
        });
        return false;
      }
      my.alert({
        content: res.message
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 选择发票
  riseChose () {
    let that = this;
    that.setData({
      rise: '',
      duty: '',
      bank: '',
      bankAccount: '',
      company_address: '',
      phone: ''
    });
    my.navigateTo({
      url: './invoice-title'
    });
  },
  onLoad(query) {
    // 页面加载时
    my.setStorageSync({
      key: 'invoice', // 缓存数据的key
      data: '', // 要缓存的数据
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    var invoice = JSON.parse(my.getStorageSync({
      key: 'invoice',
    }).data);
    if (invoice != "") {
      this.setData({
        rise: invoice.enterpriseName ? invoice.enterpriseName : '',
        duty: invoice.taxpayerNum ? invoice.taxpayerNum : ''
      })
    };
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
